package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    // 处理查询所有部门请求
    // /systemDictionaryItem/list 目前不带参数, 响应 HTML 格式内容
    @RequiredPermission(name="部门页面", expression = "systemDictionaryItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo){
        model.addAttribute("systemDictionarys", systemDictionaryService.listAll());
        // 获取所有部门数据, 存到 Model 中
        model.addAttribute("pageInfo", systemDictionaryItemService.query(qo));
        // 找视图
        return "/systemDictionaryItem/list"; // /WEB-INF/views/systemDictionaryItem/list.jsp
    }

    // /systemDictionaryItem/delete?id=id值
    // 处理查询所有部门删除的请求,希望浏览器重新发送一次请求再查询一次
    @RequiredPermission(name="员工新增/编辑", expression = "systemDictionaryItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return "redirect:/systemDictionaryItem/list"; // 让浏览器再发送一次请求 localhost/systemDictionaryItem/list
    }

    // 处理保存的请求 /systemDictionaryItem/saveOrUpdate, POST 携带参数
    @RequiredPermission(name="员工新增/编辑", expression = "systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemService.update(systemDictionaryItem);
        }else {
            systemDictionaryItemService.save(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }

    @RequestMapping("/selectByTypeId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByTypeId(Long id) {
        return systemDictionaryItemService.selectByTypeId(id);
    }

    @RequestMapping("/selectBySn")
    @ResponseBody
    public List<SystemDictionaryItem> selectBySn(String sn) {
        return systemDictionaryItemService.selectBySn(sn);
    }

    @RequestMapping("/selectByParentId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByParentId(Long id) {
        return systemDictionaryItemService.selectByParentId(id);
    }
}
